import { cloudbaseApp, development } from "./cloudbase-api";

const db = cloudbaseApp().database();
const $ = db.command.aggregate;
const _ = db.command;
const { HYDBNAME, HYTYPE } = development();

/**
 * 获取诗集列表
 * @param limit
 * @param page
 * @param filterStr
 * @returns {Promise<GetRes>}
 */
export async function getHyItem(limit, page, type = "全部类型") {
  const skip = (page - 1) * limit;
  const where = { type };
  if (type == "全部类型") {
    delete where.type;
  }
  const count = await db.collection(HYDBNAME).where(where).count();
  const { total } = count;
  const items = await db.collection(HYDBNAME).where(where).limit(limit).skip(skip).orderBy("sort", "desc").get();
  items.total = total;
  return items;
}
/**
 * 通过id值查询话语内容
 */
export function getHyId(id) {
  return db.collection(HYDBNAME).doc(id).get();
}

/**
 * 获取话语类型
 * @returns {Promise<cloudbase.database.GetRes>}
 */
export function getHyType() {
  return db.collection(HYTYPE).orderBy("sort", "asc").where({ show: true }).get();
}
